<?php
//var_dump();
class CityColorManager  extends BaseModel
{
	public $res;
	public $redis_key="";
	public function getSource()
    {
        return "city_color_manager";
    }
	public function initialize()
	{
		parent::initialize();
	}

    /**
     * 获取所有有用的区代地区
     */
	public function get_list($params,$method="list"){
        $condition = "type not in('village','town')";
        $page   = isset($params['page']) ? intval($params['page']) : 1;
        $size   = isset($params['limit']) ? intval($params['limit']) : 10;
        if(isset($params['pid_area_code']) && !empty($params['pid_area_code'])){
            $condition = "pid_area_code = '{$params['pid_area_code']}'";
        }
        if ($page > 0) {
            $page = ($page - 1) * $size;
        }
        if($method == 'list'){
            $cate_list                  = $this->find(
                array(
                    $condition,
                    'order'=>'id asc',
                    'limit'   => $size,
                    'offset'  => $page
                ))->toarray();
            return $cate_list;
        }else{
            $cate_list  = $this->count(
                array(
                    $condition,
                ));
            return $cate_list;
        }
    }



    /**
     * 区域联动
     */
    public function get_area_code($pid_area_code=0){
        if($pid_area_code){
            $conditions = "pid_area_code = {$pid_area_code}";
        }else{
            $conditions = "type='province'";
        }
        return $this->find(array($conditions));
    }


    /**
     * 获取该区域下面的所有ids
     */
    public function down_area_ids($path){
        $condition['conditions'] = "path like '{$path}%' AND type in('province','city','area','town')";
        $condition['columns'] = "id";
        $get_find_ids = $this->find($condition)->toarray();
        if($get_find_ids){
            $ids = array_column($get_find_ids,'id');
            return implode(',',$ids);
        }else{
            return -1;
        }
    }
}
